package viewers;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import models.HoughTransformModel;

/**
 * Filtered image viewer
 * @author benjamin
 	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class FilteredImageViewer extends JInternalFrame implements Observer {
	JPanel imagePanel;
	JLabel content;
	/**
	 * Sets up the image pane
	 */
	private void init(){
		imagePanel = new JPanel(new BorderLayout());
		content = new JLabel("No image loaded yet");
		content.setHorizontalAlignment(SwingConstants.CENTER);
		imagePanel.add(content,BorderLayout.CENTER);
		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		Dimension screenSize = new Dimension(gd.getDisplayMode().getWidth(),gd.getDisplayMode().getHeight());
		setBounds(new Rectangle(screenSize.width/4,0,screenSize.width/4,screenSize.width/4));
		setLayout(new BorderLayout());
		JScrollPane scroller = new JScrollPane(imagePanel);  
		this.getContentPane().add(scroller, BorderLayout.CENTER); 
		setVisible(true);
		try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
	}
	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer() {
		super();
		init();
	}
	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer(String title, boolean resizable, boolean closable,
			boolean maximizable, boolean iconifiable) {
		super(title, resizable, closable, maximizable, iconifiable);
		init();
	}

	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer(String title, boolean resizable, boolean closable,
			boolean maximizable) {
		super(title, resizable, closable, maximizable);
		init();
	}

	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer(String title, boolean resizable, boolean closable) {
		super(title, resizable, closable);
		init();
	}

	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer(String title, boolean resizable) {
		super(title, resizable);
		init();
	}

	/**
	 * (See JInteralFrame)
	 */
	public FilteredImageViewer(String title) {
		super(title);
		init();
	}


	/* event handler for the event-driven MVC pattern
	 * @see viewers.Observer#onObservation(java.lang.String, java.lang.Object)
	 */
	@Override
	public void onObservation(String id, Object value) {
		if (id.equals("LOADEDIMG")){
			ImageIcon img = new ImageIcon(((HoughTransformModel)value).getFilteredImg());
			content.setText("");
			content.setIcon(img);
		}
	}

}
